﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Linq.Expressions;
using Sisocana.Dal.Interfaces;
using Sisocana.Dal.Infrastructure;
using Sisocana.Dal.EntityModels;
using System.Data.Objects;
using Sisocana.Dal.ExternalService;
using System.ServiceModel;


namespace Sisocana.Dal
{
    public class PersonaRepository : RepositoryBase<Persona>, IPersonaRepository
    {
        public PersonaRepository()
            : this(new SisocanaRepositoryContext())
        {
        }

        public PersonaRepository(IRepositoryContext repositoryContext)
            : base(repositoryContext)
        {
        }

        public IList<PersonaDesaparecida> personasBusquedaExternaAdvanced(string url,string firstName, string lastName,string phone, DateTime missingFrom, DateTime missingTo)
        {
            SearchParameters sp = new SearchParameters();
            sp.firstName = firstName;
            sp.lastName = lastName;
            sp.phone = phone;
            
            DateTime d = new DateTime();
            if (d.CompareTo(missingFrom) == 0)
                sp.missingDateFrom = null;
            else
                sp.missingDateFrom = missingFrom;
            if (d.CompareTo(missingTo) == 0)
                sp.missingDateTo = null;
            else
                sp.missingDateTo = missingTo;

            List<Person> p = new List<Person>();
            try
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(url);

                SearchPeopleClient serviceClient = new SearchPeopleClient(binding, address);
                p = serviceClient.AdvancedSearchPerson(sp);
            }
            catch (Exception e)
            {
            }
            List<PersonaDesaparecida> ret = new List<PersonaDesaparecida>();
            foreach (Person person in p)
            {
                PersonaDesaparecida nueva = new PersonaDesaparecida();
                nueva.nombre = person.firstName;
                nueva.apellido = person.lastName;
                nueva.celular = person.phone;
                nueva.id = person.id;
                nueva.edad = person.siteId;
                nueva.fecha = person.missingDate;
                ret.Add(nueva);
            }
            return ret;
        }

        public IList<PersonaDesaparecida> personasBusquedaExternaKeyWord(string url, string keyword)
        {

            List<PersonaDesaparecida> ret = new List<PersonaDesaparecida>();
            List<Person> p = new List<Person>();
            try
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(url);

                SearchPeopleClient serviceClient = new SearchPeopleClient(binding, address);

                p = serviceClient.keywordSearchPerson(0, keyword);
            }
            catch (Exception e) { }
            foreach(Person person in p)
            {
                PersonaDesaparecida nueva = new PersonaDesaparecida();
                nueva.nombre = person.firstName;
                nueva.apellido = person.lastName;
                nueva.celular = person.phone;
                nueva.id= person.id;
                nueva.edad = person.siteId;
                nueva.fecha = person.missingDate;
                ret.Add(nueva);
            }

            return ret;
        }

        public int agregarReporte(string url, int siteId, int personId, Reporte rep)
        {

            Report r = new Report();
            r.CoordX = rep.coordX;
            r.CoordY = rep.coordY;
            r.date = rep.fecha;
            r.email = rep.email;
            r.firstName = rep.nombre;
            r.lastName = rep.apellido;
            r.message = rep.mensaje;
            r.personId = personId;
            r.phone = rep.telefono;
            r.siteId = siteId;
            r.status = rep.status;
            int result = -1;
            try
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(url);

                SearchPeopleClient serviceClient = new SearchPeopleClient(binding, address);
                result = serviceClient.addReport(r);
            }
            catch(Exception e)
            {
                result = -1;
            }
            return result;
        }

        public PersonaDesaparecidaFull completarInfoExterna(string url, int siteId, int personId)
        {
            PersonaDesaparecidaFull p = null;

            try
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(url);

                SearchPeopleClient serviceClient = new SearchPeopleClient(binding, address);
                PersonDetails resp = serviceClient.getPersonDetails(siteId, personId);
                p = new PersonaDesaparecidaFull();
                p.apellido = resp.lastName;
                p.celular = resp.phone;
                p.descripcion = resp.description;
                p.edad = resp.age;
                p.fecha = resp.missingDate;
                p.id = personId;
                p.nombre = resp.firstName;
                p.sexo = resp.genre;
                p.idSitio = siteId;
                if (resp.photo != null)
                {
                    if (resp.photo.imgByte != null)
                        p.foto.imgByte = resp.photo.imgByte;
                    if (resp.photo.imgExt != null)
                        p.foto.imgExt = resp.photo.imgExt;
                    if (resp.photo.imgName != null)
                        p.foto.imgNombre = resp.photo.imgName;
                }
                if (resp.reports != null)
                {
                    foreach (Report r in resp.reports)
                    {
                        Reporte rep = new Reporte();
                        if (r.lastName == null)
                            rep.apellido = "Unknown";
                        else
                            rep.apellido = r.lastName;
                        rep.coordX = r.CoordX;
                        rep.coordY = r.CoordY;
                        if (r.email == null)
                            rep.email = "Unknown";
                        else
                            rep.email = r.email;
                        if (r.date == null)
                            rep.fecha = new DateTime();
                        else
                            rep.fecha = r.date;
                        if (r.message == null)
                            rep.mensaje = "-";
                        else
                            rep.mensaje = r.message;
                        if (r.firstName == null)
                            rep.nombre = "Unknown";
                        else
                            rep.nombre = r.firstName;
                        if (r.status == null)
                            rep.status = "U";
                        else
                            rep.status = r.status;
                        rep.telefono = r.phone;
                        p.Reports.Add(rep);
                    }
                }
                else
                {
                    Reporte rep = new Reporte();
                    rep.apellido = "Unknown";
                    rep.email = "Unknown";
                    rep.email = "Unknown";
                    rep.fecha = new DateTime();
                    rep.mensaje = "-";
                    rep.nombre = "Unknown";
                    rep.status = "U";
                    p.Reports.Add(rep);
                }
                Direccion d = new Direccion();
                if (resp.address != null)
                {
                    d.Ciudad = resp.address.city;
                    d.CoordX = resp.address.coordX;
                    d.CoordY = resp.address.coordY;
                    d.CP = resp.address.cp;
                    d.Dir = resp.address.address;
                    d.Estado = resp.address.state;
                    d.Pais = resp.address.country;
                }
                else
                {
                    d.Ciudad = "";
                    d.CoordX = null;
                    d.CoordY = null;
                    d.CP = "";
                    d.Dir = "";
                    d.Estado = "";
                    d.Pais = "";
                }
                p.HomeAddress = d;
            }
            catch (Exception e) { }
            return p;
        }
    }
}
